package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.Admin;
import com.zjxf.base.bean.po.ProductClassify;
import com.zjxf.base.bean.ro.RoProductClassify;
import com.zjxf.base.bean.vo.VoProductClassify;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.ProductClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * controller for TProductClassify
 * code generated by jpa codegen
 *
 * @author ZhangWei
 * Created On 2021/03/23.
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "产品分类")
@RequestMapping("/productClassify/")
public class ProductClassifyController extends BaseController {

    private final ProductClassifyService productClassifyService;

    @ApiOperation(value = "添加或修改")
    @PostMapping(value = "addOrUpdate")
    public JSONResponse addOrUpdate(@Valid @RequestBody VoProductClassify.addOrUpdate voProductClassify) {
        Admin admin = (Admin) getUser();
        productClassifyService.addOrUpdate(voProductClassify, admin);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public JSONResponse delete(@RequestParam("id") Long id) throws OperationException {
        productClassifyService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "回显")
    @GetMapping("getEntity")
    public JSONResponse getEntity(@RequestParam("id") Long id) throws OperationException {
        ProductClassify productClassify = productClassifyService.getEntity(id);
        RoProductClassify.Entity roProductClassify = productClassifyService.getEntityRo(productClassify);
        return JSONResponse.success(roProductClassify);
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public JSONResponse page(@Valid @RequestBody VoProductClassify.filter filter) {
        Page<ProductClassify> productClassifyPage = productClassifyService.page(filter);
        JSONObject jsonData = productClassifyService.conversion(productClassifyPage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "集合")
    @GetMapping("adminList")
    public JSONResponse adminList() {
        List<ProductClassify> productClassifyList = productClassifyService.adminList();
        List<RoProductClassify.AdminList> roProductClassifyList = productClassifyService.conversionList(productClassifyList);
        return JSONResponse.success(roProductClassifyList);
    }

}